package cn.easyutil.veteran.core.biz;

import cn.easyutil.veteran.core.entity.BizEntity;
import cn.easyutil.veteran.core.handler.BizServiceExecuteHandler;
import cn.easyutil.veteran.core.utils.SpringUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class BizServiceImpl<M extends BizMapper<T>, T extends BizEntity> extends ServiceImpl<M, T> implements BizService<T> {

    @Override
    public <E extends IPage<T>> E page(E page, Wrapper<T> queryWrapper) {
        long size = page.getSize();
        if(size < 0){
            List<T> list = list(queryWrapper);
            IPage<T> result = new Page<>();
            result.setRecords(list);
            result.setPages(1);
            result.setTotal(list==null?0:list.size());
            result.setCurrent(1);
            result.setSize(list==null?0:list.size());
            return (E) result;
        }
        return super.page(page, queryWrapper);
    }

    @Override
    public int count(Wrapper<T> queryWrapper) {
        return super.count(queryWrapper);
    }

    @Override
    public List<T> listByIds(Collection<? extends Serializable> idList) {
        LambdaQueryWrapper<T> queryWrapper = Wrappers.lambdaQuery(this.entityClass).in(T::getId, idList);
        return list(queryWrapper);
    }

    @Override
    public T get(T t) {
        return get(Wrappers.lambdaQuery(t));
    }

    @Override
    public T get(Wrapper<T> queryWrapper) {
        return getOne(queryWrapper,false);
    }

    @Override
    public List<T> list(T t) {
        LambdaQueryWrapper<T> queryWrapper = Wrappers.lambdaQuery(t);
        return list(queryWrapper);
    }

    @Override
    public List<T> list() {
        LambdaQueryWrapper<T> queryWrapper = Wrappers.lambdaQuery(this.entityClass);
        return list(queryWrapper);
    }

    @Override
    public List<T> list(Wrapper<T> queryWrapper) {
        return super.list(queryWrapper);
    }

    @Override
    public <E extends IPage<T>> E page(E page) {
        return page(page,Wrappers.lambdaQuery(this.entityClass));
    }

    @Override
    public <E extends IPage<T>> E page(E page, T t) {
        return page(page, Wrappers.lambdaQuery(t));
    }

    @Override
    public boolean deleteByIdsNotRemove(Collection<Long> idList) {
        if(idList==null || idList.isEmpty()){
            return true;
        }
        return update(Wrappers.<T>update().set("deleted",1).in("id",idList));
    }

    @Override
    public List<T> getBySplitIds(String ids) {
        if(StringUtils.isBlank(ids)){
            return null;
        }
        Set<Long> idSet = Stream.of(ids.split(",")).filter(StringUtils::isNotBlank).map(Long::valueOf).collect(Collectors.toSet());
        if(idSet.isEmpty()){
            return null;
        }
        return listByIds(idSet);
    }

    @Override
    public T getById(Serializable id) {
        return get(Wrappers.lambdaQuery(this.entityClass).eq(BizEntity::getId,id));
    }
}
